#!/bin/sh

check_sts_annotation(){
  local EXPECTED_ANNOTATION_VALUE="$1"

  local POD_ANNOTATION="${POD_ANNOTATION:-"test-annotation"}"

  local ACTUAL_ANNOTATION
  ACTUAL_ANNOTATION="$(wait_until kubectl get sts -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME" -o jsonpath="{.spec.template.metadata.annotations.$POD_ANNOTATION}")"

  if assert_string_equal "$EXPECTED_ANNOTATION_VALUE" "$ACTUAL_ANNOTATION"
  then 
    echo "SUCCESS. Custom annotations were created"
  else
    echo "FAIL. Custom annotations did not match the expected value"
    return 1
  fi
}

check_pod_annotation(){
  local INSTANCE="$1"
  local EXPECTED_ANNOTATION_VALUE="$2"

  local POD_ANNOTATION="${POD_ANNOTATION:-"test-annotation"}"

  local ACTUAL_ANNOTATION
  ACTUAL_ANNOTATION="$(kubectl get pod -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME-$INSTANCE" -o jsonpath="{.metadata.annotations.$POD_ANNOTATION}")"

  if assert_string_equal "$EXPECTED_ANNOTATION_VALUE" "$ACTUAL_ANNOTATION"
  then 
    echo "SUCCESS. Custom annotations were created"
  else
    echo "FAIL. Custom annotations did not match the expected value"
    return 1
  fi
}

check_cronjob_annotation(){
  local EXPECTED_ANNOTATION_VALUE="$1"

  local POD_ANNOTATION="${POD_ANNOTATION:-"test-annotation"}"

  local ACTUAL_ANNOTATION
  ACTUAL_ANNOTATION="$(kubectl get cronjob -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME-backup" -o jsonpath="{.spec.jobTemplate.spec.template.metadata.annotations.$POD_ANNOTATION}")"

  if assert_string_equal "$EXPECTED_ANNOTATION_VALUE" "$ACTUAL_ANNOTATION"
  then 
    echo "SUCCESS. Custom annotations were created"
  else
    echo "FAIL. Custom annotations did not match the expected value"
    return 1
  fi
}

check_job_annotation(){
  local BACKUP_NAME="$1"
  local EXPECTED_ANNOTATION_VALUE="$2"

  local POD_ANNOTATION="${POD_ANNOTATION:-"test-annotation"}"

  local ACTUAL_ANNOTATION
  ACTUAL_ANNOTATION="$(kubectl get job -n "$CLUSTER_NAMESPACE" "$BACKUP_NAME-backup" -o jsonpath="{.spec.template.metadata.annotations.$POD_ANNOTATION}")"

  if assert_string_equal "$EXPECTED_ANNOTATION_VALUE" "$ACTUAL_ANNOTATION"
  then 
    echo "SUCCESS. Custom annotations were created"
  else
    echo "FAIL. Custom annotations did not match the expected value"
    return 1
  fi
}

check_service_annotation(){
  local INSTANCE="$([ -z "$1" ] || printf %s "-$1")"
  local EXPECTED_ANNOTATION_VALUE="$2"
  local SERVICE_ANNOTATION="${SERVICE_ANNOTATION:-"service-annotation"}"

  local ACTUAL_ANNOTATION
  ACTUAL_ANNOTATION="$(kubectl get service -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME$INSTANCE" -o jsonpath="{.metadata.annotations.$SERVICE_ANNOTATION}")"

  if assert_string_equal "$EXPECTED_ANNOTATION_VALUE" "$ACTUAL_ANNOTATION"
  then 
    echo "SUCCESS. Custom annotations were created"
  else
    echo "FAIL. Custom annotations did not match the expected value"
    return 1
  fi
}

check_distributed_logs_annotation(){
  local SERVICE="$1"
  local DISTRIBUTEDLOG_ANNOTATION_KEY="$2"
  local EXPECTED_ANNOTATION_VALUE="$3"
  
  local ACTUAL_ANNOTATION
  ACTUAL_ANNOTATION="$(kubectl get sgdistributedlogs -n "$CLUSTER_NAMESPACE" -o jsonpath="{.items[0].spec.postgresServices.${SERVICE}.annotations.${DISTRIBUTEDLOG_ANNOTATION_KEY}}")"

  if assert_string_equal "$EXPECTED_ANNOTATION_VALUE" "$ACTUAL_ANNOTATION"
  then 
    echo "SUCCESS. Custom annotations were created"
  else
    echo "FAIL. Custom annotations did not match the expected value"
    return 1
  fi
}